package com.lzp.mybatisdemo;

import com.lzp.mybatisdemo.mapper.CoffeMapper;
import com.lzp.mybatisdemo.model.Coffee;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@Slf4j
@MapperScan("com.lzp.mybatisdemo.mapper") // 扫描mapper
public class MybatisDemoApplication implements ApplicationRunner {

    @Autowired
    private CoffeMapper coffeMapper;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Coffee espresso = Coffee.builder().name("espresso")
                .price(Money.of(CurrencyUnit.of("CNY"), 20.0)).build();
        int count = coffeMapper.save(espresso);
        log.info("save {} coffee: {}", count, espresso);

        Coffee latte = Coffee.builder().name("latte")
                .price(Money.of(CurrencyUnit.of("CNY"), 30.0)).build();
        count = coffeMapper.save(latte);
        log.info("save {} coffee: {}", count, latte);

        Coffee coffee = coffeMapper.findById(latte.getId());
        log.info("query coffee: {}", coffee);
    }

    public static void main(String[] args) {
        SpringApplication.run(MybatisDemoApplication.class, args);
    }

}
